Find the
perimeter and area of the rectangle.
Input. Each line represents
a separate test and contains the length n
and the width m (1 ≤ n, m ≤ 1000)
of the
rectangle.
Output.
For
each test, print the perimeter and area of the rectangle in one line.
Sample
input |
Sample
output |
3 1 5 3 10 7 |
8 3 16 15 34 70 |
simple computations
To compute the
perimeter and area of the rectangle, use the formulas:
P = (n + m) * 2;
S = n * m
For each
test, read the input data, compute, and print the answer.
while (scanf("%d %d",
&n, &m) == 2)
{
p = 2 * (n + m);
s = n * m;
printf("%d
%d\n", p, s);
}
Algorithm realization – structures
#include <stdio.h>
struct rectangle
{
int x, y;
} a;
int GetPerimeter(struct rectangle p)
{
return 2 * (p.x + p.y);
}
int GetArea(struct rectangle p)
{
return p.x * p.y;
}
int main(void)
{
while (scanf("%d
%d", &a.x,
&a.y) == 2)
printf("%d %d\n", GetPerimeter(a), GetArea(a));
return 0;
}
Algorithm realization – classes
#include <stdio.h>
int n, m;
class Rectangle
{
public:
int x,
y;
Rectangle(int x, int y) :
x(x), y(y) {}
int
Perimeter()
{
return (x + y) * 2;
}
int
Area()
{
return x * y;
}
};
int main(void)
{
while
(scanf("%d %d", &n, &m) == 2)
{
Rectangle r(n,
m);
printf("%d
%d\n", r.Perimeter(), r.Area());
}
return 0;
}
Java realization – abstract class
import java.util.*;
abstract class Rect
{
int a, b;
abstract int Perimeter();
abstract int Area();
}
class Rectangle extends Rect
{
Rectangle(int a, int b)
{
this.a = a;
this.b = b;
}
public int Perimeter()
{
return 2 * (a + b);
}
public int Area()
{
return a * b;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner con = new
Scanner(System.in);
while(con.hasNextInt())
{
int a = con.nextInt();
int b = con.nextInt();
Rectangle r = new Rectangle(a,b);
System.out.println(r.Perimeter() + " " + r.Area());
}
con.close();
}
}
Java realization – interface
import java.util.*;
interface Rect
{
int Perimeter();
int Area();
}
class Rectangle implements Rect
{
int a, b;
Rectangle(int a, int b)
{
this.a = a;
this.b = b;
}
public int Perimeter()
{
return (a + b) * 2;
}
public int Area()
{
return a * b;
}
}
public class Main
{
public static void main(String[] args)
{
Scanner con = new
Scanner(System.in);
while(con.hasNextInt())
{
int a = con.nextInt();
int b = con.nextInt();
Rectangle r = new Rectangle(a,b);
System.out.println(r.Perimeter() + " " + r.Area());
}
con.close();
}
}